//
//  MBDIntegrationDetailViewModel.swift
//  MasterBoy
//
//  Created by kyson on 2023/5/14.
//

import Foundation

import YYText
import SwiftyJSON
import Alamofire

class MBDIntegrationDetailCommonCellModel : MBDCommonCellModel {
    var location : MBDCellLocation!
    var accessory : Bool?
}

class MBDIntegrationDetailCellModel : MBDIntegrationDetailCommonCellModel {
    
    static let topMargin : CGFloat = 18
    static let marginBetweenConsumptionAndTime : CGFloat = 15
    static let bottomMargin : CGFloat = 10
    static let consumptionLeftMargin : CGFloat = 15
    static let priceDetailLabelWidth : CGFloat = 60
    static let priceDetailLabelRightMargin : CGFloat = 15
    static let priceDetailLabelLeftMargin : CGFloat = 30

    var consumptionLayout : YYTextLayout!
    var timeLayout : YYTextLayout!
        
    var integrationDetail : MBDIntegrateExpDetailModel? {
        didSet {
            
            let consumptionAttributeString = NSMutableAttributedString(string: self.integrationDetail?.integralSource ?? "" )
            consumptionAttributeString.yy_font = UIFont.boldSystemFont(ofSize: 16)
            consumptionAttributeString.setDynamicTextColor()
           
            let consumptionContainer = YYTextContainer()
            let consumptionMaxWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDCommonCellModel.MarginLeftAndRightBackgroundView * 2 - MBDIntegrationDetailCellModel.consumptionLeftMargin - MBDIntegrationDetailCellModel.priceDetailLabelWidth - MBDIntegrationDetailCellModel.priceDetailLabelRightMargin - MBDIntegrationDetailCellModel.priceDetailLabelLeftMargin
            consumptionContainer.size = CGSizeMake(consumptionMaxWidth , CGFLOAT_MAX)
            consumptionContainer.maximumNumberOfRows = 2
            
            self.consumptionLayout = YYTextLayout(container: consumptionContainer, text: consumptionAttributeString)
            
            let timeAttributeString = NSMutableAttributedString(string: self.integrationDetail?.createTime ?? "")
            timeAttributeString.yy_font = UIFont.systemFont(ofSize: 12)
            timeAttributeString.yy_lineSpacing = 4
            timeAttributeString.setDynamicTextColor()
            
            let timeContainer = YYTextContainer()
            let timeWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDCommonCellModel.MarginLeftAndRightBackgroundView * 2 - MBDIntegrationDetailCellModel.consumptionLeftMargin - MBDIntegrationDetailCellModel.priceDetailLabelWidth - MBDIntegrationDetailCellModel.priceDetailLabelRightMargin - MBDIntegrationDetailCellModel.priceDetailLabelLeftMargin
            timeContainer.size = CGSizeMake(timeWidth , CGFLOAT_MAX)
            timeContainer.maximumNumberOfRows = 1
            
            self.timeLayout = YYTextLayout(container: timeContainer, text: timeAttributeString)
        }
    }
    
    override func cellHeight() -> CGFloat {
        let height1 = self.consumptionLayout!.textBoundingSize.height

        let height2 = self.timeLayout!.textBoundingSize.height

        return MBDIntegrationDetailCellModel.topMargin + height1 + MBDIntegrationDetailCellModel.marginBetweenConsumptionAndTime + height2 + MBDIntegrationDetailCellModel.bottomMargin
    }
    
}



class MBDIntegrationDetailViewModel : NSObject {
    
    var cellModels : Array<Any>?

    override init() {
//        self.cellModels = MBDMyIntegrationViewModel.defaultCellModels();
    }
    
    static func defaultCellModels(exps : Array<MBDIntegrateExpDetailModel> )-> Array<MBDIntegrationDetailCellModel> {
        var integrateCells = [MBDIntegrationDetailCellModel]()
        
        for i in 0..<exps.count {
            let cellModel = MBDIntegrationDetailCellModel()
            cellModel.integrationDetail = exps[i]
            integrateCells.append(cellModel)
        }
        
        for i in 0..<integrateCells.count {
            let cc = integrateCells[i] as MBDIntegrationDetailCommonCellModel
            if 0 == i {
                cc.location = .top
            } else if integrateCells.count - 1 == i {
                cc.location = .bottom
            } else {
                cc.location = .center
            }
        }

        return integrateCells
    }
    
    func doRequest(pageNum : Int = 1, completion : @escaping (_ error: Error? ,_ result : Array<Any>?,_ totalPage : Int,_ totalIntegrate : Int)->()) {
        
        MBDHttpClient.shared.request("/userIntegral/listPage",parameters: ["pageNum":String(pageNum) ],encoding: URLEncoding.default).httpClientResponse { error, response in
            if nil != error {
                completion(error,nil,0,0)
            } else {
                let rawDict = response?.rawDict as! Dictionary<String,Any>
                let rawData = rawDict["data"] as! NSDictionary
                let totalIntegralString = rawData["myIntegral"] as! String
                let totalIntegral = Int(totalIntegralString)
                let pageDataInfo = rawData["pageDataInfo"] as! NSDictionary
                let totalPage = pageDataInfo["totalPage"] as! Int
                
                let rawRows = pageDataInfo["data"]
                if rawRows is Array<Any> {
                    let rows = rawRows as! Array<Any>
                    var multiRows = Array<MBDIntegrateExpDetailModel>()

                    for row in rows {
                        let item = MBDIntegrateExpDetailModel(JSON(rawValue: row) ?? JSON(NSObject()))
                        multiRows.append(item)
                    }

                    let cellModels = MBDIntegrationDetailViewModel.defaultCellModels(exps: multiRows)

                    completion(nil,cellModels,totalPage,totalIntegral ?? 0)
                } else {
                    completion(error,nil,0,0)
                }
                
            }
        }
    }
}
